//
//  People.m
//  HelloWord
//
//  Created by feng on 15/12/3.
//  Copyright © 2015年 冯宝东. All rights reserved.
//

#import "People.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import "NSString+Base64.h"

#define RGBColor(r, g, b ,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]

#define RandColor RGBColor(arc4random(255), arc4random(255), arc4random(255),1)

@implementation People

-(id)initWithname:(NSString*)newName withAge:(int)newAge withHeight:(int)newheight
{
    self=[super init];
    if (self)
    {
       
        name=newName;
        age=newAge;
        height=newheight;
        
        
        
    }
    return self;

}

-(void)explainPeople
{
    NSLog(@" 这个人的姓名 : %@  年龄： %d 岁  身高：%d cm",name,age,height);

}
-(void)sheWillSleep
{
    NSLog(@"迎萱要去睡觉了");

}
-(void)kongzhiWithTime:(int)timeS   ScreenColor:(screenBlock)myBlock
{

    _kongzhiBlock=[myBlock copy];

    [NSTimer scheduledTimerWithTimeInterval:timeS target:self
                                   selector:@selector(kongzhiPressed:) userInfo:nil repeats:YES];
    

}
+(void)kongzhiWithTime:(int)timeS   ScreenColor:(screenBlock)myBlock
{
    [[self alloc] kongzhiWithTime:timeS ScreenColor:myBlock];
    

}
-(void)kongzhiPressed:(id)sender
{
    UIColor*colors=[People randomColor];

    _kongzhiBlock(colors);

}

+(UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 255 / 256.0 ) ; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 255 / 256.0 ) ; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
-(void)playSoundForMyBabay

{

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        NSArray*urlArray=@[@"http://music.baidutt.com/up/kwcywkkp/yypmy.mp3",@"http://music.baidutt.com/up/kwcywkkm/apspay.mp3",@"http://music.baidutt.com/up/kwcywkkp/yypma.mp3"];
        NSInteger row=arc4random()%3;
        

        NSString*url1= [urlArray objectAtIndex:row];
        NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        NSString* mp3Name = [url1 base64EncodedString];
        NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , mp3Name];
        NSFileManager* fileManger=[NSFileManager defaultManager];
        
        
        if ([fileManger fileExistsAtPath:filePath])
        {
            NSURL *musicUrl=[NSURL URLWithString:filePath];
            audioPlayer     = [[AVAudioPlayer alloc] initWithContentsOfURL:musicUrl error:nil];
            

        }else
        {
        
        
            NSURL *url = [[NSURL alloc]initWithString:url1];
            NSData * audioData = [NSData dataWithContentsOfURL:url];
            //            //将数据保存到本地指定位置
            [audioData writeToFile:filePath atomically:YES];
            
            [[NSUserDefaults  standardUserDefaults] setValue:mp3Name forKey:@"mp3Name"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            audioPlayer               = [[AVAudioPlayer alloc] initWithData:audioData error:nil];
        
        }
        

        dispatch_sync(dispatch_get_main_queue(), ^{
            
            [ audioPlayer prepareToPlay];
            audioPlayer.volume        = 0.8;
            audioPlayer.numberOfLoops = 3;
            
            [audioPlayer play];
            
            
            

            
            
        });
        

        
        
        
    });

    
    
    
    
    
    
}

-(void)stopPlaySoundForMyBabay;
{
    [audioPlayer  stop];


}

@end
